package pl.edu.pw.ee.buildcity.scheduler.strategies;

import pl.edu.pw.ee.buildcity.commons.exceptions.BuildCityRuntimeException;
import pl.edu.pw.ee.buildcity.entities.BuildActivity;
import pl.edu.pw.ee.buildcity.entities.SCMCommit;
import pl.edu.pw.ee.buildcity.scheduler.BuildContext;

import java.io.File;
import java.util.List;

/**
 * @author Mateusz
 * @since 2010-02-28
 */
public class CodeChangedBuildStrategy extends BuildStrategy {

	public CodeChangedBuildStrategy(BuildContext context, File workingCopy) {
		super(context, workingCopy);
	}

	@Override
	public void preProcess() {
		List<SCMCommit> commits = svnService.getLogBetweenHEADAndWorkingCopyRevision(workingCopy);
		buildCtx.setChanges(commits);

		BuildActivity registeredActivity = buildActivityService.registerCodeChangedBuildActivityStart(buildCtx.getBuildName(), buildCtx.getChanges());
		buildCtx.setBuildActivity(registeredActivity);
	}

	@Override
	public void performBuild() throws BuildCityRuntimeException {
		updateWorkingCopyToHEAD();
		runMaven2Build();
	}

	@Override
	public void postProcess() {
		buildPostProcessing.performPostProcessing(buildCtx);
	}

	private void updateWorkingCopyToHEAD() {
		svnService.updateToHEAD(workingCopy);
	}
}
